#!/bin/sh
# ==============================================
# Crude Jar regeneration tools.
# This code, modeled on the original 
# crude_regenerate will compile the current
# set of code, but will also download the
# latest jsr and sjsxp jars and include them 
# in the jars along with a short note to the
# effect that the licensing is more complex.

# Construct if not present the storage dir.
# and download the appropriate jars.
# --------------------------------------------
if [ ! -e "./JarDir/" ]
then
    mkdir ./JarDir
fi

cd JarDir

# Download the relevant jars and unpack them.
# ---------------------------------------------------
echo "Obtaining sjsxp code."
if [ ! -e "./sjsxp/" ]
then
    mkdir ./sjsxp
fi

cd sjsxp
wget -v https://sjsxp.dev.java.net/files/documents/3071/147264/sjsxp_102.class

# Unpack the code.  With the caveat that it will ask 
# about the license.
java -cp . sjsxp_102

# Change to the dir and unpack the jar.
cd sjsxp-1_0_2/
jar xvf sjsxp.jar
cd ../../


# Retrieve and unpack the jsr 173 library.
# -------------------------------------------
echo "Obtaining jsr173 code."
if [ ! -e "./jsr173" ]
then
    mkdir ./jsr173
fi
cd jsr173
wget -v https://stax-utils.dev.java.net/source/browse/*checkout*/stax-utils/lib/jars/jsr173_1.0_api.jar
jar xvf jsr173_1.0_api.jar
cd ../..

# Make necessary symlinks.
# ----------------------------
ln -s JarDir/sjsxp/sjsxp-1_0_2/com/ ./com
ln -s JarDir/jsr173/javax/ ./javax



# generate the license files.
# -----------------------------------------
echo "License info:" > README.License.txt
echo "All code in this jar is licensed under the GPLv2" >> README.License.txt
echo "with the exception of:" >> README.License.txt
echo " The JSR 173 APIs located in javax/ which is " >> README.License.txt
echo "  covered under the CDDL v. 1 and GPL v. 2 here: " >> README.License.txt
echo "  https://glassfish.dev.java.net/public/CDDL+GPL.html" >> README.License.txt
echo "" >> README.License.txt
echo " and the sjsxp v. 102 code located in com/" >> README.License.txt
echo "  provided under the Sun binary license agreement." >> README.License.txt


# Specify the jar contents.
# -------------------------------------------------
everything="*/*.class */*.java doc/*"
everything="$everything com/"
everything="$everything javax/"
everything="$everything README.License.txt"
echo $everything


# Basic compilation tasks.
# ---------------------------------------
echo "Building basic targets."
javac -target 5 @compile.txt &&
jar cfe Setup.jar candidateView.CandidateListRunner $everything &&
jar cfe Vote.jar electionView.ElectionRunner $everything &&
jar cfe Tabulate.jar tabulatorCtrl.TabulateRunner $everything &&
ls -ltr *.jar
